define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $window, Personal_data, Auth, GoPage, GoCheck, GoDialog, GoPicture) {
		$scope.isLoad = false;
		$scope.data = {};

				//画面加载
		$scope.$on('$ionicView.afterEnter', function(scope, status) {
			//动态设置高度
			GoPage.init_top($scope, 0, 0);

			$scope.map_height = Math.floor($window.innerWidth / 2.75);

			$scope.init_data();
		}, false);

		function setRoleText(seller, store_id, issue_type, store_issue_name, is_warehouse) {
			var role_text = '';
			if (seller == 'seller') {
				if (store_id) {
					role_text = store_issue_name;
					if (issue_type == '3' && is_warehouse == true) {
						role_text = '分仓';;
					}
				} else {
					role_text = '未申请店铺';
				}
			} else {
				role_text = "普通用户"
			}
			$scope.role_text = role_text;
		}

		// 重新获取用户信息
		$scope.init_data = function(cbComplete) {
			var Infos = Auth.get_loginInfo();

			if (!Infos) {
				GoDialog.toast('未登录');
				GoPage.go_tabPage('home');
				return;
			}

			// 账号
			var account = Infos.user_name || "";
			var end;
			if (account.length > 8) {
				account = account.substring(0, 4) + "****" + account.substring(8);
			} else if (account.length > 4) {
				account = account.substring(0, 4) + "****";
			}
			$scope.account = account;

			// 显示的用户名
			$scope.showname = Infos.user_nick;
			if (!$scope.showname) {
				$scope.showname = Infos.user_name;
			}

			// 身份
			setRoleText(Infos.user_type == 2 ? 'seller': '', Infos.store_id, Infos.store_issue, Infos.store_issue_name, Infos.is_warehouse)

			$scope.data.nickname = Infos.user_nick;

			Personal_data.get_UserInfo({
				'session': Infos.user_id
			}, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}
				$scope.isLoad = true;

				var d = res.result;
				$scope.data = d;

				setRoleText(d.seller, d.store_id, d.issue_type, Infos.store_issue_name, d.is_warehouse == 'Y')

				$scope.showname = d.nickname
				if (!$scope.showname) {
					$scope.showname = d.name;
				}

			}, null, cbComplete)
		}

		$scope.do_save = function() {
			var Infos = Auth.get_loginInfo();

			if (!Infos) {
				GoDialog.toast('未登录');
				GoPage.go_tabPage('home');
				return;
			}

			var nick = ($scope.data.nickname || "").trim();
			var email = ($scope.data.email || "").trim();
			var avatar = $scope.data.avatar_id;

			if (email && !GoCheck.checkEmail(email)) {
				GoDialog.toast('请输入正确的EMail');
				return;
			}


			Personal_data.save_UserInfo({
				'session': Infos.user_id,
				'nickname': nick,
				'email': email,
				'image_logo': avatar
			}, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}

				Auth.save_loginInfo_item('user_nick', nick);
				GoDialog.toast("保存成功");
			})
		}

		$scope.select_img = function() {

			GoPicture.upload(function(res) {
				if(!res.success) {
					GoDialog.toast(res.msg);
					return;
				}

				var r = res.result;
				var image_id = r.image_id;
				var image_src = r.image_src;
				// GoDialog.toast(r.image_id);

				$scope.data.avatar_id = image_id;
				$scope.data.avatar = image_src;
			});
		}
	}

	controller.$inject = ['$scope', '$window', 'Personal_data', 'Auth', 'GoPage', 'GoCheck', 'GoDialog', 'GoPicture'];
	
	return app.registerController("personal_datactrl",controller);
});